PostCSS Focus Within
PostCSS Focus Within lets you use the :focus-within
pseudo-class in CSS,
following the Selectors Level 4 specification.
It is the companion to the focus-within polyfill.
.my-form-field:focus-within label {
background-color: yellow;
}
.my-form-field[focus-within] label {
background-color: yellow;
}
.my-form-field:focus-within label {
background-color: yellow;
}
PostCSS Focus Within duplicates rules using the :focus-within
pseudo-class
with a [focus-within]
attribute selector, the same selector used by the
focus-within polyfill. This replacement selector can be changed using the
replaceWith
option. Also, the preservation of the original :focus-within
rule can be disabled using the preserve
option.
Usage
Add PostCSS Focus Within to your build tool:
npm install postcss-focus-within --save-dev
Node
Use PostCSS Focus Within to process your CSS:
import focusWithin from 'postcss-focus-within';
focusWithin.process(YOUR_CSS);
PostCSS
Add PostCSS to your build tool:
npm install postcss --save-dev
Use PostCSS Focus Within as a plugin:
import postcss from 'gulp-postcss';
import focusWithin from 'postcss-focus-within';
postcss([
focusWithin()
]).process(YOUR_CSS);
Webpack
Add PostCSS Loader to your build tool:
npm install postcss-loader --save-dev
Use PostCSS Focus Within in your Gulpfile:
import focusWithin from 'postcss-focus-within';
module.exports = {
module: {
rules: [
{
test: /\.css$/,
use: [
'style-loader',
{ loader: 'css-loader', options: { importLoaders: 1 } },
{ loader: 'postcss-loader', options: {
ident: 'postcss',
plugins: () => [ focusWithin() ]
} }
]
}
]
}
}
Gulp
Add Gulp PostCSS to your build tool:
npm install gulp-postcss --save-dev
Use PostCSS Focus Within in your Gulpfile:
import postcss from 'gulp-postcss';
import focusWithin from 'postcss-focus-within';
gulp.task('css', () => gulp.src('./src/*.css').pipe(
postcss([
focusWithin()
])
).pipe(
gulp.dest('.')
));
Grunt
Add Grunt PostCSS to your build tool:
npm install grunt-postcss --save-dev
Use PostCSS Focus Within in your Gruntfile:
import focusWithin from 'postcss-focus-within';
grunt.loadNpmTasks('grunt-postcss');
grunt.initConfig({
postcss: {
options: {
use: [
focusWithin()
]
},
dist: {
src: '*.css'
}
}
});
Options
preserve
The preserve
option defines whether the original selector should remain. By
default, the original selector is preserved.
focusWithin({ preserve: false });
.my-form-field:focus-within label {
background-color: yellow;
}
.my-form-field[focus-within] label {
background-color: yellow;
}
replaceWith
The replaceWith
option defines the selector to replace :focus-within
. By
default, the replacement selector is [focus-within]
.
focusWithin({ replaceWith: '.focus-within' });
.my-form-field:focus-within label {
background-color: yellow;
}
.my-form-field.focus-within label {
background-color: yellow;
}
.my-form-field:focus-within label {
background-color: yellow;
}